<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 重写call 
        // 思路:通过对象的属性来调用函数
        function fn(a, b) {
            return a + b
        }

        Function.prototype._call = function (context) {
            if (typeof context === 'object' && context !== null) {
                var property = randomString()
                context[property] = this // this即fn函数

                var args = []
                for (var i = 1; i < arguments.length; i++) {
                    args.push(arguments[i])
                }

                var res = eval('context[property](' + args + ')')
                delete context[property]
                return res
            }
        }

        Function.prototype._apply = function (context) {
            if (typeof context === 'object' && typeof context !== null) {
                var property = randomString()
                context[property] = this // this即fn函数

                var args = []
                for (var i = 0; i < arguments[1].length; i++) {
                    args.push(arguments[1][i])
                }

                var res = eval('context[property](' + args + ')')
                delete context[property]
                return res
            }
        }

        function randomString() {
            return Math.random().toString(36).substr(3, 6) + new Date().getTime().toString(36)
        }

        console.log(fn._call({}, 1, 2))
        console.log(fn._apply({}, [1, 2]))

    </script>
</body>

</html>